#include "marketserver.h"
#include <string>
#include <vector>
#include <memory>
#include <atomic>

namespace XTrade {

	MarketServer::MarketServer() :api_(nullptr)
	{

	}

	bool MarketServer::start(boost::property_tree::ptree& cfg)
	{
		api_ = XServerHandlerApi::CreateServerHandler();
		api_->register_spi(this);
		x_size_t io_thread_num = cfg.get<x_size_t>("io_thread_num", 5);
		x_size_t work_thread_num = cfg.get<x_size_t>("work_thread_num", 10);
		x_ushort_t port = cfg.get<x_ushort_t>("port", 6666);
		api_->start(io_thread_num, work_thread_num);
		return api_->listen(port);
	}

	void MarketServer::stop()
	{
		api_->stop();
		XServerHandlerApi::DestroyServerHandler(api_);
		api_ = nullptr;
	}

	void MarketServer::handle_close(const x_size_t peer)
	{

	}

	void MarketServer::handle_io_accept(const x_size_t peer)
	{

	}

	x_int_t MarketServer::handle_work_packet(const x_size_t peer, const x_buffer_t* recv_data, x_buffer_t*& send_data)
	{
		return 1;
	}

	x_int_t MarketServer::on_handle_work_packet(const x_size_t peer, const x_buffer_t* recv_data, x_buffer_t*& send_data, bool is_last)
	{
		return 0;
	}
}


